//
//  PaperItem.m
//  DoingPaper
//
//  Created by zhuxuhong on 16/7/26.
//  Copyright © 2016年 zhuxuhong. All rights reserved.
//

#import "PaperItem.h"

@implementation PaperItem

-(instancetype)initWithDict: (NSDictionary*)dict{
    if (self = [super init]) {
        _questions = [NSMutableArray new];
        [self setValuesForKeysWithDictionary:dict];
    }
    return self;
}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{
    if ([key isEqualToString:@"type_name"]) {
        _typeName = value;
    }
    else if([key isEqualToString:@"question"]){
        if ([NSStringFromClass([value class]) isEqualToString:@"__NSCFArray"]) {
            for (NSDictionary *dict in value) {
                if (dict.allKeys.count > 0) {
                    PaperQuestion *q = [[PaperQuestion alloc]initWithDict:dict];
                    [_questions addObject:q];
                }
            }
        }
        else if ([NSStringFromClass([value class]) isEqualToString:@"__NSCFDictionary"]){
            PaperQuestion *q = [[PaperQuestion alloc]initWithDict: (NSDictionary*)value];
            [_questions addObject:q];
        }
    }
}

// 收藏的试题
-(NSMutableArray *)collectedQuestions{
    NSMutableArray *arr = [NSMutableArray new];
    for (PaperQuestion *q in _questions) {
        if (q.collected) {
            [arr addObject:q];
        }
    }
    return arr;
}

@end
